package com.blame.springblog.common.utils;



import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class SecurityUtils {

    public static String encrypt(String inputPassword){
        String salt= UUID.randomUUID().toString().replace("-","");
        String encryptPassword= DigestUtils.md5DigestAsHex((salt+inputPassword).getBytes(StandardCharsets.UTF_8));
        return salt+encryptPassword;
    }

    public static Boolean verify(String inputPassword,String sqlPassword){
        if(!StringUtils.hasLength(inputPassword)){
            return false;
        }
        if(sqlPassword==null  || sqlPassword.length()!=64){
            return false;
        }
        String salt=sqlPassword.substring(0,32);
        String encryptPassword=DigestUtils.md5DigestAsHex((salt+inputPassword).getBytes(StandardCharsets.UTF_8));
        String result=salt+encryptPassword;
        return sqlPassword.equals(result);

    }



}
